var count = 0;
let network = null;
let api = null;

function getBaseInfo(app) {
  network = new app.NetWorkUtils();
  api = new app.Api();

  // 登录
  wx.login({
    success: function (res) {
      // 发送 res.code 到后台换取 openId, sessionKey, unionId
      app.globalData.jsCode = res.code;
      if (wx.getExtConfig) {
        wx.getExtConfig({
          success: function (res) {
            //console.log(res.extConfig)
            console.log('----------jsCode----------')
            console.log(app.globalData.jsCode)
            // 得到appId
            app.globalData.appId = res.extConfig.appId;
            // 获取openId
            network._post1(app, api.wxUrl, { appId: app.globalData.appId, jsCode: app.globalData.jsCode }, openIdSuccess, fail);
          }
        })
      }
    },
    fail: function (res) {
      console.log("微信登录失败")
      console.log(res)
      network._error(api.wxError, res);
    }
  })
}

// 返回openId请求结果
var openIdSuccess = function (that, res) {
  console.log(res)
  // 得到openId
  that.globalData.openId = res.data.data.openid
  that.globalData.sessionKey = res.data.data.session_key
  if (that.openIdReadyCallback) {
    that.openIdReadyCallback(res)
  }
}

// 返回openId请求失败结果
var fail = function (that, res) {
  console.log("服务器连接失败")
  console.log(res)
  network._error(api.wxError, res);
  count++;
  if (count < 3) {
    network._post1(that, api.wxUrl, { appId: that.globalData.appId, jsCode: that.globalData.jsCode }, openIdSuccess, fail);
  }
}

module.exports = {
  getBaseInfo: getBaseInfo
}
